package main

import (
	"net"
	"github.com/name5566/leaf/log"
	"wind/wind-config"
	"math/rand"
	"strconv"
	"time"
)

func main() {
	wind_config.ConfigServer("wind-config/agent.json")

	srcAddr := &net.UDPAddr{IP: net.IPv4zero, Port: 0}
	dstAddr := &net.UDPAddr{
		IP: net.ParseIP(wind_config.ServerConfig.ServerHost),
		Port: wind_config.ServerConfig.ServerPort,
	}
	conn, err := net.DialUDP("udp", srcAddr, dstAddr)
	if err != nil {
		log.Fatal("%s", err)
	}
	defer conn.Close()

	// Write Data To Server
	rand.Seed(int64(time.Now().Nanosecond()))
	conn.Write([]byte("hello "+ strconv.Itoa(rand.Intn(1000))))

	for {
		// Read Data From Server
		data := make([]byte, 1024)
		n, err := conn.Read(data)
		if err != nil {
			log.Error("read data from server err %s", err)
		}

		log.Debug("read %s from <%s>\n", data[:n], conn.RemoteAddr())
	}
}